<template>
  <div class="app-container">
    <!-- <el-card style="height: 60px">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['alarm:rule:add']"
            >新增告警配置</el-button
          >
        </el-col>
      </el-row>
    </el-card> -->

    <el-card class="bottom-table-card" v-loading="loading">
      <div style="display: flex; flex-wrap: wrap">
        <el-card
          :body-style="{ padding: '5px' }"
          v-for="(item, index) in ruleList"
          :key="index"
          style="
            width: 240px;
            margin-right: 15px;
            margin-bottom: 15px;
            position: relative;
          "
        >
          <div class="item-text">
            <span>告警:</span>
            <span class="value-text">{{ item.alarmRuleName }} </span>
          </div>
          <div class="item-text">
            <span>告警类型:</span>
            <span class="value-text">
              <dict-tag
                :options="dict.type.alarm_type"
                :value="item.alarmType"
              />
            </span>
          </div>
          <div class="item-text" v-show="item.ruleTarget">
            <span>告警对象:</span>
            <span class="value-text"
              >{{ item.ruleTarget == "person" ? "人员" : "车辆" }}
            </span>
          </div>
          <div class="item-text" v-show="item.stopMaxMinute">
            <span>最大停留时间:</span>
            <span class="value-text">{{ item.stopMaxMinute }} 分钟</span>
          </div>
          <div class="item-text" v-show="item.outMaxMinute">
            <span>出围栏最大时间:</span>
            <span class="value-text">{{ item.outMaxMinute }} 分钟</span>
          </div>
          <div class="item-text" v-show="item.oilWaste">
            <span>油量消耗阈值:</span>
            <span class="value-text">{{ item.oilWaste }} </span>
          </div>

          <div style="padding-top: 35px"></div>
          <div
            style="position: absolute; bottom: 5px; width: 90%; padding: 5px"
          >
            <el-button
              type="text"
              icon="el-icon-edit"
              v-hasPermi="['alarm:rule:edit']"
              @click="handleUpdate(item)"
              >修改</el-button
            >

            <el-switch
              style="float: right; margin-top: 9px"
              v-model="item.enabled"
              size="mini"
              :active-value="1"
              :inactive-value="0"
              active-text="启用"
              inactive-text="禁用"
              @change="switchChange(item)"
            >
            </el-switch>
          </div>
        </el-card>
      </div>
    </el-card>

    <!-- 添加或修改告警规则对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="告警规则名称" prop="alarmRuleName">
          <el-input
            v-model="form.alarmRuleName"
            placeholder="请输入告警规则名称"
          />
        </el-form-item>
        <el-form-item label="告警对象" prop="ruleTarget">
          <el-select v-model="form.ruleTarget">
            <el-option value="person" label="人员"></el-option>
            <el-option value="car" label="车辆"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="告警类型" prop="alarmType">
          <el-select
            v-model="form.alarmType"
            placeholder="请选择告警类型"
            style="width: 100%"
          >
            <el-option
              v-for="dict in dict.type.alarm_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="最大停留时间" prop="stopMaxMinute">
          <el-input
            v-model="form.stopMaxMinute"
            placeholder="请输入最大停留时间"
          />
        </el-form-item>
        <el-form-item label="出围栏最大时间" prop="outMaxMinute">
          <el-input
            v-model="form.outMaxMinute"
            placeholder="请输入出围栏最大时间"
          />
        </el-form-item>
        <el-form-item label="油量消耗阈值" prop="oilWaste">
          <el-input v-model="form.oilWaste" placeholder="请输入油量消耗阈值" />
        </el-form-item>
        <el-form-item label="是否启用" prop="enabled">
          <el-switch
            v-model="form.enabled"
            size="mini"
            :active-value="1"
            :inactive-value="0"
            active-text="启用"
            inactive-text="禁用"
          >
          </el-switch>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm"
          >确 定</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listRule,
  getRule,
  delRule,
  addRule,
  updateRule,
} from "@/api/alarm/rule";

export default {
  name: "Rule",
  dicts: ["alarm_type"],

  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 告警规则表格数据
      ruleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptId: undefined,
        alarmRuleName: undefined,
        ruleTarget: undefined,
        enabled: undefined,
        alarmType: undefined,
        stopMaxMinute: undefined,
        outMaxMinute: undefined,
        oilWaste: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        alarmRuleId: [
          { required: true, message: "告警规则id不能为空", trigger: "blur" },
        ],
        deptId: [
          { required: true, message: "部門id不能为空", trigger: "blur" },
        ],
        alarmRuleName: [
          { required: true, message: "告警规则名称不能为空", trigger: "blur" },
        ],
        ruleTarget: [
          {
            required: true,
            message: "对象人员或者车辆不能为空",
            trigger: "blur",
          },
        ],
        enabled: [
          { required: true, message: "是否启用不能为空", trigger: "blur" },
        ],
        alarmType: [
          { required: true, message: "告警类型不能为空", trigger: "change" },
        ],
        stopMaxMinute: [
          { required: true, message: "最大停留时间不能为空", trigger: "blur" },
        ],
        outMaxMinute: [
          {
            required: true,
            message: "出围栏最大时间不能为空",
            trigger: "blur",
          },
        ],
        // oilWaste: [
        //   { required: true, message: "油量消耗阈值不能为空", trigger: "blur" },
        // ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询告警规则列表 */
    getList() {
      this.loading = true;
      listRule(this.queryParams).then((response) => {
        this.ruleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        alarmRuleId: undefined,
        deptId: undefined,
        alarmRuleName: undefined,
        ruleTarget: undefined,
        enabled: undefined,
        remark: undefined,
        alarmType: undefined,
        createTime: undefined,
        stopMaxMinute: undefined,
        updateTime: undefined,
        outMaxMinute: undefined,
        createBy: undefined,
        oilWaste: undefined,
        updateBy: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.alarmRuleId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加告警规则";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const alarmRuleId = row.alarmRuleId || this.ids;
      getRule(alarmRuleId).then((response) => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改告警规则";
      });
    },
    switchChange(val) {
      updateRule(val).then((response) => {
        if (response.code === 200) {
          this.$modal.msgSuccess("状态修改成功");
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.alarmRuleId != null) {
            updateRule(this.form)
              .then((response) => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          } else {
            addRule(this.form)
              .then((response) => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const alarmRuleIds = row.alarmRuleId || this.ids;
      this.$modal
        .confirm('是否确认删除"' + (row.orderName || "已勾选") + '"的数据项?')
        .then(() => {
          this.loading = true;
          return delRule(alarmRuleIds);
        })
        .then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .finally(() => {
          this.loading = false;
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "alarm/rule/export",
        {
          ...this.queryParams,
        },
        `rule_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style lang="scss" scoped>
.item-text {
  padding: 8px;
  font-size: 14px;
  color: #606266;
  font-weight: 700;
  display: flex;
  .value-text {
    color: #606266;
    font-weight: 500;
    margin-left: 2px;
  }
}
</style>
